package com.springboot.framework.controller;

import com.springboot.framework.controller.request.ProductInsertBean;
import com.springboot.framework.controller.request.ProductUpdateBean;
import com.springboot.framework.controller.request.StatusUpdateBean;
import com.springboot.framework.enums.Errors;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.ProductModel;
import com.springboot.framework.model.UserModel;
import com.springboot.framework.service.ProductService;
import com.springboot.framework.utils.ResponseVoUtil;
import com.springboot.framework.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author SWF
 * @date 2019/6/25 15:16
 **/
@Api(description = "【商家】商品")
@RestController
@RequestMapping("/seller/product")
public class SellerProductController extends BaseController {
    @Resource
    private ProductService productService;

    @ApiOperation(value = "所有商品", notes = "所有商品")
    @GetMapping("/list")
    public ResponseVO<List<ProductModel>> listAll(@RequestParam Integer shopId, HttpServletRequest request) {
        List<ProductModel> productModels = productService.listAll(getShopId(request));
        return ResponseVoUtil.success(productModels);
    }

    @ApiOperation(value = "查询商品", notes = "查询商品")
    @GetMapping("/get")
    public ResponseVO get(@RequestParam Integer productId, HttpServletRequest request) {
        ProductModel productModel = productService.get(productId);
        if (productModel != null) {
            checkShopIdOwner(productModel.getShopId(),request);
        }
        return ResponseVoUtil.success();
    }

    @ApiOperation(value = "新增商品", notes = "新增商品")
    @PostMapping("/insert")
    public ResponseVO insert(@RequestBody @Valid ProductInsertBean bean, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        ProductModel productModel = convertProductModel(bean);
        productModel.setShopId(getShopId(request));
        productModel.setIntroduction(bean.getPromotion());
        //productModel.setCreateBy();
        return ResponseVoUtil.success(productService.insert(productModel));
    }

    @ApiOperation(value = "修改商品", notes = "修改商品")
    @PostMapping("/update")
    public ResponseVO<ProductModel> update(@RequestBody @Valid ProductUpdateBean bean, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        // 校验店铺
        checkShopIdOwner(bean.getShopId(), request);
        ProductModel productModel = convertProductModel(bean);
        productModel.setIntroduction(bean.getPromotion());
        return ResponseVoUtil.success(productService.update(productModel));
    }

    @ApiOperation(value = "修改商品状态", notes = "修改商品状态")
    @PostMapping("/updateStatus")
    public ResponseVO<ProductModel> updateStatus(@RequestBody @Valid StatusUpdateBean bean, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        Integer shopId = getShopId(request);
        return ResponseVoUtil.success(productService.updateStatus(bean.getId(), bean.getStatus(), shopId));
    }

    @ApiOperation(value = "删除商品", notes = "删除商品")
    @DeleteMapping("/delete")
    public ResponseVO<ProductModel> delete(@RequestParam Integer id, HttpServletRequest request) {
        Integer shopId = getShopId(request);
        productService.delete(id,shopId);
        return ResponseVoUtil.success();
    }

    private void checkShopIdOwner(Integer shopId, HttpServletRequest request) {
        UserModel userModel = super.getSessionUser(request);
        if (!shopId.equals(userModel.getShopId())) {
            throw new BusinessException(Errors.SYSTEM_NO_ACCESS);
        }
    }

    private Integer getShopId(HttpServletRequest request){
        UserModel userModel = super.getSessionUser(request);
        if(userModel.getShopId() == null){
            throw new BusinessException("您还没有商铺");
        }
        return userModel.getShopId();
    }

    private ProductModel convertProductModel(ProductInsertBean bean) {
        ProductModel productModel = new ProductModel();
        BeanUtils.copyProperties(bean, productModel);
        return productModel;
    }

    private ProductModel convertProductModel(ProductUpdateBean bean) {
        ProductModel productModel = new ProductModel();
        BeanUtils.copyProperties(bean, productModel);
        return productModel;
    }
}
